package ui.popup 
{
	import data.*;
	import events.GCEvent;
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	import ui.button.GCButton;
	import ui.textfield.GCStylizedText;
	
	/**
	 * A popup that displays a certain cost that the player must
	 * pay in order to perform an action.
	 * 
	 * Will be used primarily for traveling from one city to another.
	 */
	public class GCCostPopup extends Sprite 
	{
		private var _payCostButton:GCButton;
		private var _cancelButton:GCButton;
		private var _textDisplay:GCStylizedText;
		
		private var _message:String;
		private var _costs:Dictionary;
		private var _costText:String;
		
		/**
		 * Constructor
		 * @param	text
		 * @param	costs
		 */
		public function GCCostPopup(text:String, costs:Dictionary) 
		{
			_payCostButton = new GCButton(onPayCost);
			_cancelButton = new GCButton(onCancel);
			_textDisplay = new GCStylizedText();
			
			_message = text;
			_costs = costs;
			_costText = "";
			
			init();
		}
		
		private function init():void
		{			
			this.addChild(_textDisplay);
			this.addChild(_payCostButton);
			this.addChild(_cancelButton);
			
			for (var obj:Object in _costs)
			{
				var id:uint = obj as uint;
				var quantity:uint = _costs[id];
				_costText += String(quantity) + " " + GCItemData.getNameFromId(id) + "\n";
			}
			//_message += ("\n\n" + _costText);
			
			_payCostButton.loadGraphic(GCResources.plate, 100, 30);
			_payCostButton.loadText("Pay Cost!");
			_payCostButton.x;
			_payCostButton.y = _textDisplay.y + _textDisplay.height;

			_cancelButton.loadGraphic(GCResources.plate, 100, 30);
			_cancelButton.loadText("Cancel");
			_cancelButton.x = _payCostButton.x + _payCostButton.width + 10;
			_cancelButton.y = _payCostButton.y;
			
			// Popup background
			var back:Sprite = GCResources.getMaskedCanvasBackground(this.width, this.height);
			this.addChildAt(back, 0);
			
			this.addChild(_textDisplay);
			this.addChild(_payCostButton);
			this.addChild(_cancelButton);
			
			_textDisplay.text = _message;
			_textDisplay.setBgStyle(GCStylizedText.STYLE_NONE, this.width, 200);
		}
		
		//----------------------------------------------------------------------
		//	PRIVATE FUNCTIONS
		//----------------------------------------------------------------------

		/**
		 * Cleans up the popup before exiting
		 */
		private function onExitCleanup():void
		{
			// Disable the buttons
			_cancelButton.disable();
			_payCostButton.disable();
		}
		
		//----------------------------------------------------------------------
		//	EVENT HANDLER
		//----------------------------------------------------------------------
		
		/**
		 * Handles the event when cost to travel is paid
		 * @param	event
		 */
		private function onPayCost(event:MouseEvent):void
		{
			// Clean up the popup
			onExitCleanup();			
			
			dispatchEvent(new Event(GCEvent.CONFIRM_MENU_EVENT));
		}
		
		/**
		 * Handles the event when travel payment is canceled
		 * @param	event
		 */
		private function onCancel(event:MouseEvent):void
		{
			// Clean up the popup
			onExitCleanup();
			
			dispatchEvent(new Event(GCEvent.CANCEL_MENU_EVENT));
		}		
		
	}
}